<%@page import="com.grom.po.Security"%>
<%@page import="java.util.Map"%>
<%@page import="com.grom.po.Department"%>
<%@page import="com.grom.util.SecurityUtil"%>
<%@page import="com.grom.po.GcmsUser"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/js/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/js/themes/icon.css">
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.6.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery.easyui.min.js"></script>

<script type="text/javascript">
$(function(){
	$('#loginInfoEdit').dialog({  
	    title : 'Brand 录入',  
	    modal: true,  
	    collapsible : true,    
	    resizable : true   
	});  
	$('#loginInfoEdit').dialog('close');
	$('#employees').dialog({  
	    modal:true  
	});  
	$('#employees').dialog('close');
	$('#win').window({  
	    width:600,  
	    height:400,  
	    modal:true  
	});
	$('#win').window('close'); 
});


function showAddDialog(){
	$('input').each(function(){
		$(this).val('');
	});
	$('select').each(function(){
		$(this).val('');
	});
	$('#loginInfoEdit').dialog('open');
}

function loginInfoEdit(){
	$('brandForm').submit();
	$('#loginInfoEdit').dialog('close');
}

function viewDetail(){
	var itemId =  $('#tt').datagrid('getSelected').itemid;
	$.ajax({
	    url: '<%=request.getContextPath()%>/DepartmentAction/getDetail.do?itemid='+itemId,
	    type: 'GET',
	    timeout: 1000,
	    error: function(){
	        alert('Error getting response!');
	    },
	    success: function(json){
	        var user = eval(json);
	        $('#ID').val(user.ID);
	        $('#departmentName').val(user.departmentName);
	        $('#remark').val(user.remark);
			$('#loginInfoEdit').dialog('open');
	    }
	});
}

function doDelete(){
	var itemId =  $('#tt').datagrid('getSelected').itemid;
	if (itemId){  
        $.messager.confirm('Confirm','Are you sure you want to remove this user?',function(r){  
            if (r){  
				$('#deleteId').val(itemId);
				$('#deleteForm').submit();
            }  
        });  
    } 
}
function doSearch(){
	$('#searchForm').submit();
}

function viewEmployee(){
	var itemId =  $('#tt').datagrid('getSelected').itemid;
	$.ajax({
	    url: '<%=request.getContextPath()%>/DepartmentAction/getEmployee.do?itemid='+itemId,
	    type: 'GET',
	    timeout: 1000,
	    error: function(){
	        alert('Error getting response!');
	    },
	    success: function(json){
	    	var user = eval(json);
	    	var htmls = "本部门下含有的人员包括：";
	    	for(var i=0 ;i < user.length ; i++){
	    		htmls += "<div style='padding:5px;'>userName : "+user[i].userName+"</div>";
	    	}
	        $('#employees').html(htmls);
	        $('#employees').dialog("open");
	    }
	});
}

function viewFirstAccess(){
	var itemId =  $('#tt').datagrid('getSelected').itemid;
	$.ajax({
	    url: '<%=request.getContextPath()%>/DepartmentAction/getBindSec.do?itemid='+itemId,
	    type: 'GET',
	    timeout: 1000,
	    error: function(){
	        alert('Error getting response!');
	    },
	    success: function(json){
	    	var securities = eval(json);
	    	for(var i=0 ;i < securities.length ; i++){
	    		$("#"+securities[i].ID + '_bind').css("display","none");
	    		$("#"+securities[i].ID + '_remove').css("display","");
	    	}
	    }
	});
	$('#win').window('open'); 
}

function doBind(securityId){
	var itemId =  $('#tt').datagrid('getSelected').itemid;
	window.location.href = "<%=request.getContextPath()%>/DepartmentAction/doBind.do?securityId="
			+securityId+"&departmentId="+itemId;
}

function doRemove(securityId){
	var itemId =  $('#tt').datagrid('getSelected').itemid;
	window.location.href = "<%=request.getContextPath()%>/DepartmentAction/doRemove.do?securityId="
			+securityId+"&departmentId="+itemId;
}
</script>
</head>
<body>
<div id="win" iconCls="icon-save" title="修改用户权限">  
  	<ul class="easyui-tree">  
       <li>  
           <span>ROOT</span>  
           <ul>  
           	   <%
           	Map<String, List<Security>>   securityMaps = (Map<String, List<Security>> )request.getAttribute("securityMaps");
           	List<Security> roots = securityMaps.get("ROOT");
           	for(Security security : roots){
           		%>
           		<li>  
                   <span><%=security.getSecurityName() %> - 
                   <a id="<%=security.getID() %>_bind" href="#" onclick="doBind('<%=security.getID() %>')">未绑定</a>
                   <a id="<%=security.getID() %>_remove" href="#" style="display: none" onclick="doRemove('<%=security.getID() %>')">移除</a>
                   </span>  
                   <ul>  
                   		<%
                   		List<Security> secs = securityMaps.get(security.getID());
                   		if (secs!=null) {
                   		for(Security sec : secs){
                   			%>
                   			<li><span><%=sec.getSecurityName() %>
                   			-
                   			<a id="<%=sec.getID() %>_bind" href="#" onclick="doBind('<%=sec.getID() %>')">未绑定</a>
                   			<a id="<%=sec.getID() %>_remove" href="#" style="display: none" onclick="doRemove('<%=sec.getID() %>')">移除</a>
                   			</span></li> 
                   			<%
                   		}
                   		}
                   		%>
                   </ul>  
               </li>  
           		<%
           	}
           	   %>
           </ul>  
       </li>  
   </ul>  
</div>  

<div id="employees" title="My Dialog" style="width:400px;height:200px;font-size: 30px;">  
    Dialog Content. 
</div> 
<div style="display: none;">
	<form name="deleteForm" id="deleteForm" action="<%=request.getContextPath() %>/DepartmentAction/doDelete.do">
		<input type="text" name="deleteId" id="deleteId"/>
	</form>
</div>
<div style="font-size: 10px;">
	<form id="searchForm" name="searchForm" action="<%=request.getContextPath() %>/DepartmentAction/conditionSelect.do" method="post">
      <label>部门名称：</label>  
      <input type="text" name="userName_s" id="userName_s" ></input>  
      <a href="#" class="easyui-linkbutton" iconCls="icon-search" plain="true" onclick="doSearch()">search</a>
    </form>
</div>
<table id="tt" class="easyui-datagrid"  singleSelect="true"
        title="DataGrid with Toolbar" iconCls="icon-save"  
        toolbar="#tb">  
    <thead>  
        <tr>  
            <th field="itemid" width="80">Item ID</th>  
            <th field="departmentName" width="80">部门名称</th>  
            <th field="remark" width="80" align="right">备注</th>   
        </tr>  
    </thead>  
    <tbody>     
    	<%
    	List<Department> allDepartments = (List<Department>)request.getAttribute("allDepartments");
    		for(Department department : allDepartments){
    	%>
        <tr>                             
            <td><%=department.getID() %></td>              
            <td><%=department.getDepartmentName() %></td>              
            <td><%=department.getRemark() %></td>                          
        </tr>  
    	<%
    		}
    	%>                         
    </tbody>
</table>  
<div id="tb">  
   <%
	if(session.getAttribute("USER")!=null){
%>
    <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="showAddDialog()">Add</a>  
    <a href="#" class="easyui-linkbutton" iconCls="icon-cut" plain="true" onclick="viewDetail()">view/update</a>  
    <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="doDelete()">Delete</a> 
    <a href="#" class="easyui-linkbutton" iconCls="icon-cut" plain="true" onclick="viewEmployee()">查看人员</a> 
    <a href="#" class="easyui-linkbutton" iconCls="icon-back" plain="true" onclick="viewFirstAccess()">一级菜单</a> 
    <a href="#" class="easyui-linkbutton" iconCls="icon-back" plain="true" onclick="viewSecondAccess()">二级菜单</a> 
<%
	}
%>
</div>  




<!-- 编辑 -->  
<div id="loginInfoEdit"  
    style="padding: 5px; width: 300px; height: 250px;">  
    <h5 id="loginInfoEdit_message" style="color: red;"></h5>  
    <div class="ToolTip_Form" id="table_loginInfoEdit" onkeydown="if(event.keyCode==13){loginInfoEdit();}">  
       <form id="departmentForm" name="departmentForm" action="<%=request.getContextPath() %>/DepartmentAction/doSave.do" method="post">
       <input type="hidden" name="ID" id="ID" value="" />
       <ul>    
            <li>  
                <label>部门名称：</label>  
                <input type="text" class="easyui-validatebox" name="departmentName" id="departmentName" maxlength="60" required="true"></input>  
            </li>  
            <li>  
                <label>备注：</label>  
                <input type="text" class="easyui-validatebox" name="remark" id="remark" maxlength="60" ></input>  
            </li> 
            <li>  
             	<a href="#" class="easyui-linkbutton" onclick="$('#departmentForm').submit();">保存</a>
            	<a href="#" class="easyui-linkbutton" onclick="$('#loginInfoEdit').dialog('close');">放弃操作</a>
            </li>  
        </ul>  
       </form>
    </div>  
</div>  
</body>
</html>